package com.runshu.newsmartark.smartbox;

import android.content.Context;
import android.util.Log;

import com.runshu.newsmartark.activity.NewMainactivity;
import com.runshu.newsmartark.activity.NewMainactivity;

import java.util.Timer;
import java.util.TimerTask;

public class SmartBoxHelper {
    public static SmartBoxHelper instance;

    public static SmartBoxHelper getInstance() {
        if (instance == null) {
            instance = new SmartBoxHelper();
        }
        return instance;
    }

    public enum BoxScanState {
        //准备就绪
        STATE_READY,
        //正在盘点
        STATE_WORKING,
        //正在停止盘点
        STATE_STOPING,
        //正在停止，停止后重新盘点
        STATE_STOPING_TOSCAN
    }

    public enum BoxAction {
        CLOSE_DOOR_SCAN,//关门盘点指令
        STOP_SCAN,//停止
        OPEN_DOOR_TIMMER_FINISH,//计时结束
        CLOSE_DOOR_TIMMER_FINISH,//计时结束
        WATCH_DOG_TIMMER//看门狗计时器

    }

    public static BoxScanState state = BoxScanState.STATE_READY;
    public Timer timer = new Timer();
    public Timer resetTimer = new Timer();

    public void startTimer() {
        if (timer != null) {
            timer.cancel();
        }
        timer = new Timer();
        timer.schedule( new TimerTask() {
            @Override
            public void run() {
                if (NewMainactivity.ctx.isCloseDoor()) {
                    setState( BoxAction.CLOSE_DOOR_TIMMER_FINISH );
                } else {
                    setState( BoxAction.OPEN_DOOR_TIMMER_FINISH );
                }
            }
        }, 5000 );
    }

    public void startResetTimer() {
        if (resetTimer != null) {
            resetTimer.cancel();
        }
        resetTimer = new Timer();
        resetTimer.schedule( new TimerTask() {
            @Override
            public void run() {
             setState( BoxAction.WATCH_DOG_TIMMER );
            }
        }, 20000 );
    }

    public void reset() {
        state = BoxScanState.STATE_READY;
        Log.e( "box_state", "reset_boxstate-------" + state );
    }

    public void setState(BoxAction action) {
        switch (state) {
            case STATE_READY:
                switch (action) {
                    case CLOSE_DOOR_SCAN:
                        startInnerScan( NewMainactivity.ctx, 0 );
                        startResetTimer();
                        state = BoxScanState.STATE_WORKING;
                        break;
                }
                break;
            case STATE_WORKING:
                switch (action) {
                    case STOP_SCAN:
                        stopInnerScan( NewMainactivity.ctx, 0 );
                        startTimer();
                        state = BoxScanState.STATE_STOPING;
                        break;
                    case WATCH_DOG_TIMMER:
                        startInnerScan( NewMainactivity.ctx, 0 );
                        startResetTimer();
                        break;
                }
                break;
            case STATE_STOPING:
                switch (action) {
                    case CLOSE_DOOR_TIMMER_FINISH:
                    case OPEN_DOOR_TIMMER_FINISH:
                        state = BoxScanState.STATE_READY;
                        break;
                    case CLOSE_DOOR_SCAN:
                        state = BoxScanState.STATE_STOPING_TOSCAN;
                        break;
                }
                break;
            case STATE_STOPING_TOSCAN:
                switch (action) {
                    case CLOSE_DOOR_TIMMER_FINISH:
                        startInnerScan( NewMainactivity.ctx, 0 );
                        state = BoxScanState.STATE_WORKING;
                        startResetTimer();
                        break;
                    case OPEN_DOOR_TIMMER_FINISH:
                        state = BoxScanState.STATE_READY;
                        break;
                }
                break;
        }
        Log.e( "box_state", "boxstate-------" + state );
    }


    public void openLightAndDoor(Context ctx, int delay) {
        SmartBoxIntentService.operateBox( SmartBoxIntentService.ACTION_OPEN_LIGHT, delay, ctx );
        SmartBoxIntentService.operateBox( SmartBoxIntentService.ACTION_OPEN_DOOR, delay, ctx );
    }

    public void openDoor(Context ctx, int delay) {
        SmartBoxIntentService.operateBox( SmartBoxIntentService.ACTION_OPEN_DOOR, delay, ctx );
    }

    public void closeDoor(Context ctx, int delay) {
        SmartBoxIntentService.operateBox( SmartBoxIntentService.ACTION_CLOSE_DOOR, delay, ctx );
    }

    //关灯
    public void closeLigth(Context ctx, int delay) {
        SmartBoxIntentService.operateBox( SmartBoxIntentService.ACTION_CLOSE_LIGHT, delay, ctx );
    }

    public void openLight(Context ctx, int delay) {
        SmartBoxIntentService.operateBox( SmartBoxIntentService.ACTION_OPEN_LIGHT, delay, ctx );
    }

    public void startScan(Context ctx) {
        if (NewMainactivity.ctx.isCloseDoor()) {
            setState( BoxAction.CLOSE_DOOR_SCAN );
        }
//        SmartBoxIntentService.operateBox( SmartBoxIntentService.ACTION_START_SCAN, delay, ctx );
    }

    public void stopScan(Context ctx) {
        setState( BoxAction.STOP_SCAN );
//        SmartBoxIntentService.operateBox( SmartBoxIntentService.ACTION_END_SCAN, delay, ctx );
    }

    private void startInnerScan(Context ctx, int delay) {
        SmartBoxIntentService.operateBox( SmartBoxIntentService.ACTION_START_SCAN, delay, ctx );
    }

    public void stopInnerScan(Context ctx, int delay) {
        SmartBoxIntentService.operateBox( SmartBoxIntentService.ACTION_END_SCAN, delay, ctx );
    }
    public void startScanA(Context ctx, int delay) {
        SmartBoxIntentService.operateBox( SmartBoxIntentService.ACTION_START_SCAN_A, delay, ctx );


    }
}
